/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : backbone router 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-18 10:59:14
 * @filename : static/mnm/src/routers/router.js
 * @version : 
 * @description : 
 */
define(function(require, exports, module){
	var Backbone, Workspace,cacheObj={};

	Backbone = require('backbone');
        /**
         * #name/type/tab
         * name : 界面模块名称，非seajs模块名称
         * type : part 界面交给组件维护 / whole 界面交给系统维护 
         * tab  : type为part时有效，组件维护必要参数
         */   
	Workspace = Backbone.Router.extend({

                initialize: function(options) {
                      var tthis = this;
                      this.route(/.*/, "all", function(){tthis.routeUrl('index','whole')});
                      this.route(":name/:type", "modules", function(name,type){tthis.routeUrl(name,type)});
                      this.route(":name/:type/tab:num", "modules", function(name,type,num){tthis.routeUrl(name,type,num)});
                },

	        /*	
                routes: {
                     "index/:type" : "index",
                     "joinus/:type" : "joinus",
                     "joinus/:type/tab:num" : "joinus",
		},
                */

                routeUrl : function(name,type,num) {
                      var obj = {
                             name : name,
                             type : type,
                             num  : num
                          };             
                      this.dispatcher(obj); 
                },

                dispatcher : function(obj) {
                      this.routesDoFuncBefore(obj.type);
                      if (cacheObj[obj.name]) {
                          cacheObj[obj.name].render(obj); 
                      } else {
			  require.async('../views/'+obj.name,function(module){
		  	     if (module) { 
                               var s = new module();
			   	   s.render(obj);                           
			       cacheObj[obj.name] = s; 
                             } else {
                               //window.location.href = '#index/whole'; 
                             }
			  });

                      }
                },
 
                routesDoFuncBefore : function(type) {
                     if (type == 'whole') {
                         $('.page').hide();
                     }
                
                     $('#body_loading').show();
    
                     if ($('.page').length > 10) {
                         var dom = $('.page').eq(0),
                             cacheId = dom.attr('id');
                             dom.remove();
                         delete cacheObj[cacheId];
                     }
                }
	 });

	module.exports = Workspace;
})

